/*
 * Copyright (c) 2013   Tobias Welther
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package de.tudarmstadt.kom.androidsensorapi.sensormanagement.sensorlistenerholder;

/**
 * Created by Tobias Welther
 */
public class SensorHolderDequeueThread extends  Thread{

    /**
     * This is the Holder to dequeue.
     */
    private final SensorListenerHolder holder;

    /**
     * This tells if the Thread is paused or not.
     */
    private boolean isPaused = true;

    /**
     * This tells that the Thread should pause after emptying the Queue of the Holder.
     */
    private boolean pauseOnEmpty = false;

    /**
     * If this is set to true, the Thread will shutdown.
     * It is unrecoverable afterwards.
     */
    private boolean shutdown = false;


    /**
     * Creates a new Dequeue Thread that dequeues the Holder passed.
     * The Thread is NOT started Yet! Call {@link #start()} to start it.
     * The Thread is Paused from Start. To start it, call {@link #continueWork()}
     *
     * @param holder to dequeue
     */
    public SensorHolderDequeueThread(final SensorListenerHolder holder) {
        this.holder = holder;
    }


    /**
     * The thread is paused when the Queue of the Holder is emptied.
     */
    public void pauseWork(){
        this.pauseOnEmpty = true;
    }


    /**
     * The Thread will continue working
     */
    public void continueWork(){
        this.isPaused = false;
        this.pauseOnEmpty = false;
    }


    /**
     * Shuts down the Thread after the next loop.
     * No further looping will be done afterwards.
     *
     * WARNING: This is a final action! Recovery afterwards is not possible.
     */
    public void shutdown(){
        shutdown = true;
    }


    @Override
    public void run() {
        super.run();

        if(shutdown) return;

        while(true){
            if(shutdown) return;

            try {
                Thread.sleep(5);

                if(isPaused || (pauseOnEmpty && holder.queueIsEmpty())){
                    if(!isPaused) isPaused = true;
                    Thread.sleep(50);
                    //Just have a longer Break to not overdue work and flood the poor CPU.
                }else{
                    holder.callSensorEvent();
                }

            } catch (InterruptedException e) {
                //not needed.
            }
        }
    }
}
